var express = require('express'),
    app = express(),
    axios = require('axios'),
    path = require('path'),
    bodyParser = require('body-parser')

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.use('/', express.static(__dirname + '/dist'))

app.get('(\/|\/index|\/home)', function (req, res) {
  res.sendfile(path.join(__dirname, '/dist/index.html'))
})

app.get('/share', function (req, res) {
  res.sendfile(path.join(__dirname, '/dist/share.html'))
})

app.get('/download', function (req, res) {
  res.sendfile(path.join(__dirname, '/dist/download.html'))
})

app.get('/detail', function (req, res) {
  res.sendfile(path.join(__dirname, '/dist/detail.html'))
})

app.get('/register', function (req, res) {
  res.sendfile(path.join(__dirname, '/dist/register.html'))
})

var url = 'http://mtm.mebonda.com:8090/mbd/stg'

// 获得筛选 dict
app.get('/get/dict', function (req, res) {
  axios({
    url: url + '/dict/all/',
    method: 'post',
    responseType: 'json',
    data: {
      app: 2
    }
  }).then((response) => {
    res.json(response.data)
  }).catch((e) => {
    console.log(e)
    res.json({
      err: true
    })
  })
})

// 获得订单详情
app.get('/detail/:id', function (req, res) {
  var params = req.params,
      id = parseInt(params.id)

  if (id) {
    axios({
      url: url + '/transportInfo/find/transport',
      method: 'post',
      responseType: 'json',
      data: {
        transportId: id
      }
    }).then((response) => {
      res.json(response.data)
    }).catch((e) => {
      console.log(e)
      res.json({
        err: true
      })
    })
  } else {
    res.json({
      err: true
    })
  }
})

// 获得运输列表数据
app.get('/transport/list', function (req, res) {
  axios({
    url: url + '/transportInfo/list/vechicleRoute',
    method: 'post',
    responseType: 'json',
    data: {}
  }).then((response) => {
    res.json(response.data)
  }).catch((e) => {
    console.log(e)
    res.json({
      err: true
    })
  })
})

// 搜索订单
app.post('/search', function (req, res) {
  axios({
    url: url + '/transportInfo/list/vechiclecontact',
    method: 'post',
    responseType: 'json',
    data: req.body
  }).then((response) => {
    res.json(response.data)
  }).catch((e) => {
    console.log(e)
    res.json({
      err: true
    })
  })
})

// 分享者的注册信息
app.post('/share/register', function (req, res) {
  axios({
    url: url + '/user/findShareRegister',
    method: 'post',
    responseType: 'json',
    data: req.body
  }).then((response) => {
    res.json(response.data)
  }).catch((e) => {
    res.json({
      err: true,
      msg: e
    })
  })
})

// 收手机验证码
app.get('/send/code/:number', function (req, res) {
  var phoneNumber = req.params.number

  axios({
    url: url + '/main/nonlogin/sendcode',
    method: 'post',
    responseType: 'json',
    data: {
      phone: phoneNumber,
      type: '1'
    }
  }).then((response) => {
    res.json(response.data)
  }).catch((e) => {
    console.log(e)
    res.json({
      err: true
    })
  })
})

// 分享页面注册的新用户
app.post('/user/nonlogin/register', function (req, res) {
  axios({
    url: url + '/user/nonlogin/register',
    method: 'post',
    responseType: 'json',
    data: req.body
  }).then((response) => {
    res.json(response.data)
  }).catch((e) => {
    console.log(e)
    res.json({
      err: true
    })
  })
})

var server = app.listen(9001, function () {
  var host = server.address().address
  var port = server.address().port
})